<?php
session_start();

if(isset($_REQUEST['onsub'])){
    if( $_REQUEST['onsub']=='ok')
    {
          //require_once dirname(__FILE__) . '/../includes/common.inc.php';
          require_once dirname(__FILE__) . '/../includes/data.cfg.php';
          require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';
        
          
          $export_name = BRANCH.'_'.'Products'.date('YmdHis');
          header("Content-type:application/vnd.ms-excel");
          header("Content-Disposition:attachment;filename=$export_name.xls");
        
        
          //require_once dirname(__FILE__) . '/includes/pdoorm.inc.php';
          
          
          $sql="SELECT `name`,type,price FROM ns_v1sample";
          $dataarr = getall($sql,1);
          echo"<table border='1'><tr>";
          echo " <td colspan='3' height='30px' align='center'><h1>".$export_name."</h1></td></tr><tr height='27' style='font-weight:bold;'>";
          echo   "<td>Name</td>";
          echo   "<td>Type</td>";
          echo   "<td>Price</td>";
          echo  "</tr>";  
        
          if(is_array($dataarr))
          {
            foreach ($dataarr as $dr)
            {
               echo" <tr>";        
               echo   "<td>".$dr['name']."</td>";
               echo   "<td>".$dr['type']."</td>";
               echo   "<td>".$dr['price']."</td>";
               echo  "</tr>";      
            }
          }
          echo"</table>";
          die;
    }

}


if(isset($_REQUEST['onsubpack'])){
    if( $_REQUEST['onsubpack']=='ok')
    {
          //require_once dirname(__FILE__) . '/../includes/common.inc.php';
          require_once dirname(__FILE__) . '/../includes/data.cfg.php';
          require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';
        
          
          $export_name = BRANCH.'_'.'Package'.date('YmdHis');
          header("Content-type:application/vnd.ms-excel");
          header("Content-Disposition:attachment;filename=$export_name.xls");
        
        
          //require_once dirname(__FILE__) . '/includes/pdoorm.inc.php';
          
          
          $sql="SELECT e_oid,`name`,orderInc,type,price FROM ns_v1package";
          $dataarr = getall($sql,1);
          $sqlnum = "SELECT MAX(cc.vnum) as vnum FROM
(SELECT COUNT(*) AS vnum,package_id FROM ns_v1packageitem GROUP BY  package_id) AS cc";
          $numarr = getall($sqlnum,1);
          
          
          echo"<table border='1'><tr>";
          echo " <td colspan='4' height='30px' align='center'><h1>".$export_name."</h1></td></tr><tr height='27' style='font-weight:bold;'>";
          echo   "<td align='center'>Name</td>";
          echo   "<td align='center'>Order Type</td>";
          echo   "<td align='center'>Type</td>";
          echo   "<td align='center'>Price</td>";
          echo   "<td colspan=\"{$numarr[0]['vnum']}\" align='center'>Contained Products</td>";
          echo  "</tr>";  
        
          if(is_array($dataarr))
          {
            foreach ($dataarr as $dr)
            {
            	$sqlsub = "SELECT p.package_id,s.`name` FROM ns_v1packageitem AS p,ns_v1sample AS s WHERE p.sample_id = s.e_oid and p.package_id = '{$dr['e_oid']}'";
            	$subnum = count_num($sqlsub);
            	$subarr = getall($sqlsub,1);
               echo" <tr>";        
               echo   "<td>".$dr['name']."</td>";
               echo   "<td>".$dr['orderInc']."</td>";
               echo   "<td>".$dr['type']."</td>";
               echo   "<td>".$dr['price']."</td>";
               
               if(is_array($subarr))
               {
               	  foreach($subarr as $sdr)
               	  {
               	  	 echo   "<td>".$sdr['name']."</td>";
               	  }
               }
               
               $shengyu = $numarr[0]['vnum'] - $subnum;
               for($ij=0; $ij<$shengyu; $ij++ )
               {
               	 echo   "<td>&nbsp;</td>";
               }
               
               echo  "</tr>";      
            }
          }
          echo"</table>";
          die;
    }

}


if(isset($_REQUEST['onsubsour'])){
    if( $_REQUEST['onsubsour']=='ok')
    {
          //require_once dirname(__FILE__) . '/../includes/common.inc.php';
          require_once dirname(__FILE__) . '/../includes/data.cfg.php';
          require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';
        
          
          $export_name = BRANCH.'_'.'source'.date('YmdHis');
          header("Content-type:application/vnd.ms-excel");
          header("Content-Disposition:attachment;filename=$export_name.xls");
          
          
          echo"<table border='1'><tr>";
          echo " <td  height='30px' align='center'><h1>".$export_name."</h1></td></tr><tr height='27' style='font-weight:bold;'>";
          echo   "<td align='center'>Name</td>";
          echo  "</tr>";  
        
                $sqlsub = "SELECT * FROM ns_isource";
                $dataarr = getall($sqlsub,1);
                
          if(is_array($dataarr))
          {
            foreach ($dataarr as $dr)
            {
               echo" <tr>";        
               echo   "<td>".$dr['name']."</td>";
               echo  "</tr>";      
            }
          }
          echo"</table>";
          die;
    }

}


if(isset($_REQUEST['onsubpp'])){
    if( $_REQUEST['onsubpp']=='ok')
    {
          //require_once dirname(__FILE__) . '/../includes/common.inc.php';
          require_once dirname(__FILE__) . '/../includes/data.cfg.php';
          require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';
        
          
          $export_name = BRANCH.'_'.'package'.date('YmdHis');
          header("Content-type:application/vnd.ms-excel");
          header("Content-Disposition:attachment;filename=$export_name.xls");
          
          
          echo"<table border='1'><tr>";
          echo " <td  height='30px' align='center' colspan='8'><h1>".$export_name."</h1></td></tr><tr height='27' style='font-weight:bold;'>";
          echo   "<td align='center'>Products Name</td>";
          echo   "<td align='center'>Price</td>";
          echo   "<td align='center'>quantity</td>";
          echo   "<td align='center'>total</td>";
          echo   "<td align='center'>total(Inc. Sales Tax)</td>";
          echo   "<td align='center'>Package Name</td>";
          echo   "<td align='center'>Package Price</td>";
          echo   "<td align='center'>Order Type</td>";
          echo  "</tr>";  
        
                $sqlsub = "SELECT ap.`name` AS product_name,ap.price AS price,ap.quantity,(ap.price*ap.quantity) AS total,bp.`name` AS package_name,bp.price AS package_price,bp.orderInc AS order_type FROM
(SELECT pm.*,sam.`name`,sam.price FROM ns_v1packageitem AS pm,ns_v1sample AS sam WHERE pm.sample_id = sam.e_oid) AS ap,ns_v1package AS bp WHERE ap.package_id =bp.e_oid
ORDER BY ap.package_id DESC";
                $dataarr = getall($sqlsub,1);
                
          if(is_array($dataarr))
          {
            foreach ($dataarr as $dr)
            {
            	$taxtotal = round($dr['total']*(1+GST),2);
               echo" <tr>";        
               echo   "<td>".$dr['product_name']."</td>";
               echo   "<td>".$dr['price']."</td>";
               echo   "<td>".$dr['quantity']."</td>";
               echo   "<td>".$dr['total']."</td>";
               echo   "<td>".$taxtotal."</td>";
               echo   "<td>".$dr['package_name']."</td>";
               echo   "<td>".$dr['package_price']."</td>";
               echo   "<td>".$dr['order_type']."</td>";
               echo  "</tr>";      
            }
          }
          echo"</table>";
          die;
    }

}
//include 'header.php';
//header("Content-type: text/html; charset=utf-8");

?>
 <br>
 <div>
    <fieldset>
    <legend>Products</legend>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
        <label><input type="hidden" name="onsub" id="onsub"  value="ok">
        <input type="submit" value="download"></label>
      </form>
    </fieldset>
    </fieldset>

    <fieldset>
    <legend>Package</legend>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
        <label><input type="hidden" name="onsubpack" id="onsubpack"  value="ok">
        <input type="submit" value="download"></label>
      </form>
    </fieldset>
    </fieldset>
    
    <fieldset>
    <legend>source</legend>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
        <label><input type="hidden" name="onsubsour" id="onsubsour"  value="ok">
        <input type="submit" value="download"></label>
      </form>
    </fieldset>
    </fieldset>
    
    <fieldset>
    <legend>PackageProduct</legend>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
        <label><input type="hidden" name="onsubpp" id="onsubpp"  value="ok">
        <input type="submit" value="download"></label>
      </form>
    </fieldset>
    </fieldset>               
 </div>